import api from '../api'
export const getDict = () => {
  return new Promise((resolve, reject) => {
    // 获取所有数据字典
    const dict = {};

    let roles = flat($vue.$plugins.getLStorage('USER_INFO').menuList)
    dict['roles'] = {};
    // 设置权限
    roles.forEach(v => {
      dict['roles'][v.code] = v;
    });
    api.getdataDictionary().then(res => {
      res.list.forEach(v => {
        dict[v.type] = dict[v.type] || {}
        dict[v.type][v.value] = v
      })
      store.commit('SET_DICT', dict);
        resolve();
      });
    })
};

function flat (arr = [], key = 'children') {
  let a = []
  arr.forEach(v => {
    if (Array.isArray(v[key]) && v[key].length > 0) {
      a = [...a, ...flat(v[key])]
      v[key] = null
      a.push(v)
    } else {
      a.push(v)
    }
  })
  return a
}